let {Card, Texas} = require('Poker')
let {Global} = require('Global')


cc.Class({
    extends: cc.Component,

    properties: {
        seats: cc.Node,
        actorPrefab: cc.Prefab,
        publicCards: cc.Node,
        dealer: cc.Node,

        emptySeat: cc.Node,
        shuffle_ani: cc.Node,
        mySeat: null,
        publicBetPanel: cc.Node

    },

    // use this for initialization
    onLoad() {
        this.publicCards = this.publicCards.getComponent('TxPublicCard')
        this.shuffle_ani = this.shuffle_ani.getComponent(sp.Skeleton)
        this.shuffle_ani.setAnimation(0, null, false)

        // this.seatPool = new cc.NodePool()
        // for (let i = 0; i < 6; i++) {
        //     let playerSit = cc.instantiate(this.actorPrefab)
        //     this.seatPool.put(playerSit)
        // }
        /* 设置一个空位置 */
        this.generatorSeatIfNeed()
    },

    /**
     * 用户坐下
     * 
     * @param {any} user
     */
    sit(user) {
        let players = this.seats.getChildren()
        if (user) {
            if (this.emptySeat) {
                let actor = this.emptySeat.getComponent('TxSeat')
                actor.user = user
                if (user.id === Global.user._id) {
                    this.mySeat = this.emptySeat.getComponent('TxSeat')
                }
                this.emptySeat = null
            }
            this.generatorSeatIfNeed()
        }
    },

    generatorSeatIfNeed() {
        let players = this.seats.getChildren()
        if (players.length < 4 && !this.emptySeat) {
            let playerSit = cc.instantiate(this.actorPrefab)
            let actor = playerSit.getComponent('TxSeat')
            this.seats.addChild(playerSit)
            this.emptySeat = playerSit
            this.updateSeatFlag()
        }
    },

    updateSeatFlag() {
        if (Global.user._id != Global.room.dealerId) {
            if (this.emptySeat)
                this.emptySeat.getComponent('TxSeat').showBaseMoneyIfNeed()
        }
    },

    stand(id) {
        let seat = this._findSeat(id)
        if (seat) {
            seat.user = null
            seat.node.parent = null
        }
        this.generatorSeatIfNeed()
    },

    callCard(info) {
        let {playerId, cardId} = info
        switch (playerId) {
            case 0:
                this.publicCards.addCard(Card.fromId(cardId))
                break
            default:
                if (playerId != Global.room.dealerId) {
                    let seat = this._findSeat(playerId)
                    if (seat) {
                        seat.callCard(Card.fromId(cardId))
                        break
                    }
                } else {
                    this.dealer.addCard(Card.fromId(cardId))
                }
                break

        }

    },

    addCardsEveryone(data) {
        for (let item of data) {
            let {playerId, cardId} = item
            let seat = this._findSeat(playerId)
            if (seat) {
                seat.callCard(Card.fromId(cardId))
            }
        }
    },

    redouble(info) {
        let {playerId, money } = info
        if (!playerId) {
            if (this.mySeat) {
                this.mySeat.redouble(money)
            } else {
                this.dealer.double(money)
            }
        } else {
            if (Global.room.dealerId === playerId) {
                this.dealer.double(money)
            } else {
                let seat = this._findSeat(playerId)
                if (seat) {
                    seat.redouble(money)
                }
            }
        }
    },

    shuffle() {
        this.shuffle_ani.node.active = true
        this.shuffle_ani.setAnimation(1, 'animation', false)
    },

    _findSeat(id) {
        let allSeats = this.seats.getChildren()
        for (let item of allSeats) {
            let seat = item.getComponent('TxSeat')
            if (seat.user && seat.user.id === id) {
                return seat
            }
        }

    },

    waiting() {
        this.dealer.waiting()
    },

    playing() {
        this.hidenPublicBetPanel()
        this.dealer.playing()
    },

    begin() {
        this.dealer.begin()
    },

    end(rlt) {
        let seats = this.seats.getChildren()
        for (let seat of seats) {
            let txseat = seat.getComponent('TxSeat')
            if (txseat.user) {
                let cards = txseat.cards
                if (cards.length) {
                    txseat.flipCards()
                    let resultCards = cards.concat(this.publicCards.cards)
                    let quotity = Texas.getCardsQuotiety(resultCards)
                    txseat.play(quotity.k)
                }
            }
        }

        let cards = this.dealer.cards
        if (cards.length) {
            let dealerResultCards = cards.concat(this.publicCards.cards)
            let dquotity = Texas.getCardsQuotiety(dealerResultCards)
            this.dealer.play(dquotity.k)
            this.dealer.flipCards()
        }

        this.scheduleOnce(() => {
            let seats = this.seats.getChildren()
            for (let seat of seats) {
                let ss = seat.getComponent('TxSeat')
                if (ss.user) {
                    ss.clear()
                }
            }
            this.publicCards.clear()
            this.dealer.clear()
            this.endBet()
        }, 5)

    },

    betting() {
        if (this.mySeat) {
            this.mySeat.showReBetmenu()
        }
        if (Global.user._id === Global.room.dealerId) {
            this.dealer.betting()
        }
    },

    endBet() {
        if (this.mySeat) {
            this.mySeat.hidenReBetmenu()
        }

        if (Global.user._id === Global.room.dealerId) {
            this.dealer.endBet()
        }
    },

    showPublicBetPanel(countDown) {
        let pas = this.publicBetPanel.getComponent('TxPublicPanel')
        pas.startBet(countDown)
        this.publicBetPanel.active = true
    },

    publicBet(info) {
        let pas = this.publicBetPanel.getComponent('TxPublicPanel')
        pas.bet(info)
    },

    hidenPublicBetPanel() {
        let pas = this.publicBetPanel.getComponent('TxPublicPanel')
        pas.stopCount()
        this.publicBetPanel.active = false
    },

    outBetResult(money) {
        this.publicBetPanel.active = true
        let pas = this.publicBetPanel.getComponent('TxPublicPanel')
        pas.updateResult(money)
        this.scheduleOnce(() => {
            this.hidenPublicBetPanel()
            pas.clear()
        }, 2)
    },

    configPublicPanel() {
        let pas = this.publicBetPanel.getComponent('TxPublicPanel')
        pas.updateConfig()
    }

});
